package com.svms.alerts;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.EventQueues;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Window;

import com.svms.service.vo.AlertVO;
import com.svms.service.vo.SpareAlertVO;
import com.svms.service.vo.VehicleAlertVO;
import com.svms.web.factory.SvmsWebFactory;

public class ManageQuickAlertsCtrl extends GenericForwardComposer {

	private Listbox lstbx_all_alerts;
	private Window window_quick_alerts;

	ListModelList<String> lst_model_all_alert_strings = null;

	public ManageQuickAlertsCtrl() {
		super();
	}

	@Override
	public ComponentInfo doBeforeCompose(Page page, Component parent,
			ComponentInfo compInfo) {
		System.out.println("Invoike doBeforeCompose");
		refreshAllAlertsList();
		return super.doBeforeCompose(page, parent, compInfo);
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		System.out.println("Invoike doAfterCompose");
		super.doAfterCompose(comp);
		EventQueues.lookup("vehicleAlertEventQueue", EventQueues.DESKTOP, true)
				.subscribe(new EventListener<Event>() {
					@Override
					public void onEvent(Event event) throws Exception {
						System.out
								.println("Event vehicleAlertEventQueue Invoked");
						refreshAllAlertsList();
						onCreate$window_quick_alerts();
					}
				});
	}

	private void refreshAllAlertsList() {

		List<AlertVO> allAllerts = SvmsWebFactory.alertService().getAllAlerts();
		System.out.println("allAllerts " + allAllerts);
		List<String> alertStrings = new ArrayList<String>();

		for (AlertVO alertVO : allAllerts) {
			if (alertVO instanceof VehicleAlertVO) {
				VehicleAlertVO vehicleAlert = (VehicleAlertVO) alertVO;
				alertStrings.add("Vehicle Number "
						+ vehicleAlert.getVehicleNumber()
						+ " has "
						+ vehicleAlert.getItemID()
						+ " Due on "
						+ new SimpleDateFormat("dd-MMM-yyyy")
								.format(vehicleAlert.getDueDate()));
				lst_model_all_alert_strings = new ListModelList<String>(
						alertStrings);
			} else if (alertVO instanceof SpareAlertVO) {
				// Logic for alert will go here
			}
		}
	}

	public void onCreate$window_quick_alerts() {
		System.out.println("Invoike on create quick alersts");
		lstbx_all_alerts.setModel(lst_model_all_alert_strings);
		lstbx_all_alerts.setItemRenderer(new ListitemRenderer<String>() {
			@Override
			public void render(Listitem item, String data, int index)
					throws Exception {
				final String alertText = (String) data;
				Listcell lc = new Listcell(alertText);
				lc.setParent(item);
			}
		});
	}

}
